home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / gnat-3.05- / gnat-3 / gnat-3.05-i486-linux-elf-bin / rts / a-sequio.adb < prev    next >
Encoding:
Text File  |  1996-06-07  |  7.1 KB  |  226 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                    A D A . S E Q U E N T I A L _ I O                     --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.13 $                             --
  10. --                                                                          --
  11. --     Copyright (C) 1992,1993,1994,1995 Free Software Foundation, Inc.     --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. --  This is the generic template for Sequential_IO, i.e. the code that gets
  37. --  duplicated. We absolutely minimize this code by either calling routines
  38. --  in System.File_IO (for common file functions), or in System.Sequential_IO
  39. --  (for specialized Sequential_IO functions)
  40.  
  41. with Interfaces.C_Streams; use Interfaces.C_Streams;
  42. with System;
  43. with System.File_Control_Block;
  44. with System.File_IO;
  45. with Unchecked_Conversion;
  46.  
  47. package body Ada.Sequential_IO is
  48.  
  49.    package FIO renames System.File_IO;
  50.    package FCB renames System.File_Control_Block;
  51.    package SIO renames System.Sequential_IO;
  52.  
  53.    SU : constant := System.Storage_Unit;
  54.  
  55.    subtype AP is FCB.AFCB_Ptr;
  56.    subtype FP is SIO.File_Type;
  57.  
  58.    function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
  59.    function To_SIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
  60.  
  61.    -----------
  62.    -- Close --
  63.    -----------
  64.  
  65.    procedure Close (File : in out File_Type) is
  66.    begin
  67.       FIO.Close (AP (File));
  68.    end Close;
  69.  
  70.    ------------
  71.    -- Create --
  72.    ------------
  73.  
  74.    procedure Create
  75.      (File : in out File_Type;
  76.       Mode : in File_Mode := Out_File;
  77.       Name : in String := "";
  78.       Form : in String := "")
  79.    is
  80.    begin
  81.       SIO.Create (FP (File), To_FCB (Mode), Name, Form);
  82.    end Create;
  83.  
  84.    ------------
  85.    -- Delete --
  86.    ------------
  87.  
  88.    procedure Delete (File : in out File_Type) is
  89.    begin
  90.       FIO.Delete (AP (File));
  91.    end Delete;
  92.  
  93.    -----------------
  94.    -- End_Of_File --
  95.    -----------------
  96.  
  97.    function End_Of_File (File : in File_Type) return Boolean is
  98.    begin
  99.       return FIO.End_Of_File (AP (File));
  100.    end End_Of_File;
  101.  
  102.    ----------
  103.    -- Form --
  104.    ----------
  105.  
  106.    function Form (File : in File_Type) return String is
  107.    begin
  108.       return FIO.Form (AP (File));
  109.    end Form;
  110.  
  111.    -------------
  112.    -- Is_Open --
  113.    -------------
  114.  
  115.    function Is_Open (File : in File_Type) return Boolean is
  116.    begin
  117.       return FIO.Is_Open (AP (File));
  118.    end Is_Open;
  119.  
  120.    ----------
  121.    -- Mode --
  122.    ----------
  123.  
  124.    function Mode (File : in File_Type) return File_Mode is
  125.    begin
  126.       return To_SIO (FIO.Mode (AP (File)));
  127.    end Mode;
  128.  
  129.    ----------
  130.    -- Name --
  131.    ----------
  132.  
  133.    function Name (File : in File_Type) return String is
  134.    begin
  135.       return FIO.Name (AP (File));
  136.    end Name;
  137.  
  138.    ----------
  139.    -- Open --
  140.    ----------
  141.  
  142.    procedure Open
  143.      (File : in out File_Type;
  144.       Mode : in File_Mode;
  145.       Name : in String;
  146.       Form : in String := "")
  147.    is
  148.    begin
  149.       SIO.Open (FP (File), To_FCB (Mode), Name, Form);
  150.    end Open;
  151.  
  152.    ----------
  153.    -- Read --
  154.    ----------
  155.  
  156.    procedure Read (File : in File_Type; Item : out Element_Type) is
  157.       Siz  : constant size_t := (Item'Size + SU - 1) / SU;
  158.       Rsiz : size_t;
  159.  
  160.    begin
  161.       FIO.Check_Read_Status (AP (File));
  162.  
  163.       --  For non-definite type or type with discriminants, read size and
  164.       --  raise Program_Error if it is larger than the size of the item.
  165.  
  166.       if not Element_Type'Definite
  167.         or else Element_Type'Has_Discriminants
  168.       then
  169.          FIO.Read_Buf
  170.            (AP (File), Rsiz'Address, size_t'Size / System.Storage_Unit);
  171.  
  172.          --  In the case of a non-definite type, make sure the length is OK.
  173.          --  We can't do this in the variant record case, because the size is
  174.          --  based on the current discriminant, so may be apparently wrong.
  175.  
  176.          if not Element_Type'Has_Discriminants and then Rsiz > Siz then
  177.             raise Program_Error;
  178.          end if;
  179.  
  180.          FIO.Read_Buf (AP (File), Item'Address, Rsiz);
  181.  
  182.       --  For definite type without discriminants, use actual size of item
  183.  
  184.       else
  185.          FIO.Read_Buf (AP (File), Item'Address, Siz);
  186.       end if;
  187.    end Read;
  188.  
  189.    -----------
  190.    -- Reset --
  191.    -----------
  192.  
  193.    procedure Reset (File : in out File_Type; Mode : in File_Mode) is
  194.    begin
  195.       FIO.Reset (AP (File), To_FCB (Mode));
  196.    end Reset;
  197.  
  198.    procedure Reset (File : in out File_Type) is
  199.    begin
  200.       FIO.Reset (AP (File));
  201.    end Reset;
  202.  
  203.    -----------
  204.    -- Write --
  205.    -----------
  206.  
  207.    procedure Write (File : in File_Type; Item : in Element_Type) is
  208.       Siz : constant size_t := (Item'Size + SU - 1) / SU;
  209.  
  210.    begin
  211.       FIO.Check_Write_Status (AP (File));
  212.  
  213.       --  For non-definite types or types with discriminants, write the size
  214.  
  215.       if not Element_Type'Definite
  216.         or else Element_Type'Has_Discriminants
  217.       then
  218.          FIO.Write_Buf
  219.            (AP (File), Siz'Address, size_t'Size / System.Storage_Unit);
  220.       end if;
  221.  
  222.       FIO.Write_Buf (AP (File), Item'Address, Siz);
  223.    end Write;
  224.  
  225. end Ada.Sequential_IO;
  226.